﻿using AutoMapper;
using DailyApp.API.ApiResponses;
using DailyApp.API.DTOs;
using DailyApp.API.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DailyApp.API.Controllers
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UsersController : ControllerBase
    {

        private readonly STJCAMContext db;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_db"></param>
        public UsersController(STJCAMContext _db, IMapper _mapper)
        {
            db = _db;
            mapper = _mapper;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="accountInfoDTO">注册信息</param>
        /// <returns>ResultCode = -1账号已存在；ResultCode = 1注册成功;ResultCode = -99注册失败;</returns>
        [HttpPost]
        public IActionResult Reg(AccountInfoDTO accountInfoDTO)
        {
            //响应数据
            ApiResponse res = new ApiResponse();
            //业务
            try
            {
               var dbBadge= db.EfmUsers.Where(t => t.Badge == accountInfoDTO.Badge).FirstOrDefault();
                if (dbBadge != null)
                {
                    res.ResultCode = -1;//账号已存在
                    res.Msg = "账号已存在";
                    return Ok(res);
                }

                //DTO->UserInfo
                EfmUser efmUser=mapper.Map<EfmUser>(accountInfoDTO);
                //EfmUser efmUser = new EfmUser()
                //{
                //    Badge = accountInfoDTO.Badge,
                //    Dept = accountInfoDTO.Dept,
                //    Password = accountInfoDTO.password,
                //    Site = "",
                //    Name = "",
                //    Cname = "",
                //    Permission = "",
                //    CreateBy = "",
                //    CreateTime = DateTime.Now,
                //};
                db.EfmUsers.Add(efmUser);
                int result=db.SaveChanges();
                if(result == 1)
                {
                    res.ResultCode = 1;
                    res.Msg = "账号注册成功";
                }
                else
                {
                    res.ResultCode = -99;
                    res.Msg = "账号注册失败";
                }
            }
            catch (Exception ex)
            {
                res.ResultCode = -99;
                res.Msg = "账号注册失败";
            }
            return Ok(res);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="Badge">工号</param>
        /// <param name="pwd">密码</param>
        /// <returns>登录信息</returns>
        [HttpGet]
        public IActionResult Login(string Badge,string pwd)
        {
            ApiResponse res=new ApiResponse();
            try
            {
                var dnAccountInfo=db.EfmUsers.Where(t=>t.Badge==Badge&&t.Password==pwd).FirstOrDefault();
                if(dnAccountInfo == null)
                {
                    res.ResultCode= -1;//账号密码错误
                    res.Msg = "账号密码错误";
                    return Ok(res);
                }
                //if (dnAccountInfo.Password != pwd){
                //    res.ResultCode = -2;//密码错误
                //    res.Msg = "密码错误";
                //    return Ok(res);
                //}
                res.ResultCode = 1;//登录成功
                res.Msg = "登录成功";
                res.ResultData = dnAccountInfo;
                return Ok(res);
            }
            catch (Exception ex)
            {
                res.ResultCode = -99;
                res.Msg = ex.Message;
            }
            return Ok(res);
        }
    }
}
